package com.example.day07_tab;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tl;
    private ViewPager vp;
    List<Fragment> list = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    MyFragAdapter myFragAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list.add(FragFactory.createFg(FragFactory.FRAGMENT_HOME));//首页
        list.add(FragFactory.createFg(FragFactory.FRAGMENT_RECOMMEND));//推荐
        list.add(FragFactory.createFg(FragFactory.FRAGMENT_ATTENTION));//关注

        titles.add("首页");
        titles.add("推荐");
        titles.add("关注");

        myFragAdapter = new MyFragAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,list,titles);
        vp.setAdapter(myFragAdapter);
        tl.setupWithViewPager(vp);

    }

    private void initView() {
        tl = (TabLayout) findViewById(R.id.tl);
        vp = (ViewPager) findViewById(R.id.vp);
    }
}